#!/bin/bash
pkg=""
pkglist=""
init_path=/root

function parse_params() {
	if [ $# -eq 0 ] || [ "$1" = "-h" ]; then
		cat <<EOF
	Usage: bash build_indocker.sh [Options]
	Options:
	    --packages
		Specify the package name. The value can be one package or list of packages as format of "pkg1,pkg2,pkg3..."
	    --path
		Specify the path for running the script. The default is /root.
EOF
	fi

	yum install util-linux -y 2>&1
	args=$(getopt -o p: -al packages:,path: -- "$@")
	if [ $? -ne 0 ]; then
		echo "Terminating..."
		exit
	fi
	eval set -- "${args}"

	while [ -n "$1" ]
	do
		case "$1" in
		-p|--packages) pkglist=$2; shift 2;;
		--path) init_path=$2; shift 2;;
		--) break;;
		*) echo "unknown args:${args}"
			exit 1
		esac
	done
}

function check_file_and_dir() {
	if [ ! -s "${init_path}"/*.repo ];then
		echo "repo file does not exist in docker!"
		exit 1
	fi
}

function config_yum() {
	for file in `ls /etc/yum.repos.d/`
	do
		mv /etc/yum.repos.d/${file} /etc/yum.repos.d/${file}.bak
	done
	cp ${init_path}/*repo /etc/yum.repos.d/
        if [ $? -ne 0 ]; then
		echo "copying yum repo file failed!"
		for file in `ls /etc/yum.repos.d/*.bak`
		do
			old_name=`echo ${file} | sed 's/.bak//g'`
			mv ${file} ${old_name}
		done
		exit 1
	fi

	yum makecache &>/dev/null
	if [ $? -ne 0 ]; then
		echo "yum makecache failed!"
		exit 1
	fi

	# prepare basic command packages
	echo "Downloading tools for build, please wait..."
	yum install rpm-build 'dnf-command(download)' -y &> /dev/null
	if [ $? -eq 0 ]; then
		echo "Download tools succeed!!"
	else
		echo "Download tools failed!"
		exit 1
	fi
}

function check_build()
{
	for pkg in ${pkglist//,/ }
	do
		mkdir -p ${init_path}/build_result/all_srcrpm
		# download source rpm
		echo "Downloading source rpm of ${pkg}, please wait..."
		yum download --source ${pkg} -y > download_src_res 2>&1
		if [ $? -eq 0 ] &>/dev/null;then
			echo "Download ${pkg} source file succeed!"
			echo "${pkg}" >> ${init_path}/build_result/succeed_download_src_list
			mkdir -p ${init_path}/build_result/success_log/${pkg}
			mv download_src_res ${init_path}/build_result/success_log/${pkg}/
			mv ${pkg}*src.rpm ${init_path}/build_result/all_srcrpm/
		else
			echo "Download ${pkg} source file failed!"
			echo "${pkg}" >> ${init_path}/build_result/failed_download_src_list
			mkdir -p ${init_path}/build_result/failed_log/${pkg}
			mv download_src_res ${init_path}/build_result/failed_log/${pkg}/
			continue
		fi
		# download dependencies
		echo "Downloading dependencies of ${pkg}, please wait..."
		rpm -ivh ${init_path}/build_result/all_srcrpm/${pkg}*src.rpm > download_dep_res 2>&1
		yum-builddep ${init_path}/rpmbuild/SPECS/${pkg}*.spec -y > download_dep_res 2>&1
		if [ `cat download_dep_res | grep "Complete!"` == "Complete!" ] &>/dev/null;then
			echo "Install ${pkg} build dependencies succeed!"
			echo "${pkg}" >> ${init_path}/build_result/succeed_download_dep_list
			mv download_dep_res ${init_path}/build_result/success_log/${pkg}/
		else
			echo "Install ${pkg} build dependencies failed!"
			echo "${pkg}" >> ${init_path}/build_result/failed_download_dep_list
			mkdir -p ${init_path}/build_result/failed_log/${pkg}
			mv download_dep_res ${init_path}/build_result/failed_log/${pkg}/
			continue
		fi
		# start building
		echo "Start building ${pkg}, please wait..."
		rpmbuild --rebuild ${init_path}/build_result/all_srcrpm/${pkg}*src.rpm > self_build_res 2>&1
		if [ $? -eq 0 ]; then
			echo "Package ${pkg} build succeed!"
			echo "${pkg}" >> ${init_path}/build_result/succeed_build_list
			mv self_build_res ${init_path}/build_result/success_log/${pkg}/
		else
			echo "Package ${pkg} build failed!"
			echo "${pkg}" >> ${init_path}/build_result/failed_build_list
			mkdir -p ${init_path}/build_result/failed_log/${pkg}
			mv self_build_res ${init_path}/build_result/failed_log/${pkg}/
			continue
		fi
	done
}

parse_params $@
config_yum
check_file_and_dir
check_build
